package shuashuami.hb.com.avtivity;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;

import com.scrat.app.selectorlibrary.ImageSelector;

import java.util.ArrayList;
import java.util.List;

import shuashuami.hb.com.adapter.ComplaintAdatper;
import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.http.BUploadComplaintHttp;

public class BComplaintActivity extends AbActivity {

    private GridView gridView;
    private ComplaintAdatper complaintAdatper;
    private List<String> imgs;
    private Button btnSubmit;
    private EditText edtId, edtContent;

    private static final int REQUEST_CODE_SELECT_IMG = 1;
    private static final int MAX_SELECT_COUNT = 9;

    private String shopId;
    private String content;

    @Override
    protected void initUI() {
        setAbContentView(R.layout.activity_bcomplaint);
        gridView = (GridView) findViewById(R.id.gv_complaint);
        btnSubmit = (Button) findViewById(R.id.btn_complaint_submit);
        edtId = (EditText) findViewById(R.id.edt_complaint_id);
        edtContent = (EditText) findViewById(R.id.edt_complaint_content);
    }

    @Override
    protected void initData() {
        setTitleBar("投诉");
        setLeftView();
        setRightTextView("投诉记录");
        initAdapter();
    }

    private void initAdapter() {
        imgs = new ArrayList<>();
        imgs.add(R.mipmap.ssm_icon_127 + "");
        complaintAdatper = new ComplaintAdatper(this, imgs);
        gridView.setAdapter(complaintAdatper);
    }

    private void upload() {
        shopId = edtId.getText().toString();
        content = edtContent.getText().toString();
        BUploadComplaintHttp uploadComplaintHttp = new BUploadComplaintHttp(this);
        uploadComplaintHttp.uploadImg(shopId, content, imgs);
    }

    @Override
    protected void initListener() {
        setRightTextViewListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openActivity(BComplaintListActivity.class);
            }
        });
        setLeftViewListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == imgs.size() - 1) {
                    if (imgs.size() <= 9) {
                        ImageSelector.show(BComplaintActivity.this, REQUEST_CODE_SELECT_IMG, MAX_SELECT_COUNT);
                    }
                }
            }
        });
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                upload();
            }
        });
    }

    private void showContent(Intent data) {
        List<String> paths = ImageSelector.getImagePaths(data);
        if (!paths.isEmpty()) {
            for (int i = 0; i < paths.size(); i++) {
                imgs.add(0, paths.get(i));
            }
            complaintAdatper.notifyDataSetChanged();
            return;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_SELECT_IMG) {
            showContent(data);
            return;
        }

        super.onActivityResult(requestCode, resultCode, data);
    }


}
